#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

// int g_unval;
// int g_val = 100;
//
//void fun()
//{
//  printf("fun begin");
// // exit(1);
//  printf("fun end");
//}

int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    printf("子进程begin\n");
    //execl("/usr/bin/ls", "ls", "-a", "-l", NULL);
    //execl("./other", "other", NULL);
   // execl("usr/bin/python3", "python", "other.py", NULL);
    //execl("/usr/bin/bash", "bash", "other.sh", NULL);
    
     char *const argv[] = {
      (char* const)"ls",
      (char* const)"-a",
      (char* const)"-l",
      NULL
    };
    
    char* const env [] ={
      (char* const)"MYENV=1122334455",
      NULL
    };
    execlp("ls", "ls", "-al", NULL);
    execv("/usr/bin/ls", argv);
    execle("/usr/bin/ls", "ls", "-a", "-l", env);
    execvp("ls",argv);
    execve("/usr/bin/ls",argv, env);
    printf("子进程end\n");
  }

  printf("父进程\n");
  return 0;



  //pid_t id = fork();
  //if(id == 0)
  //{
  //  //子进程
  //  int cnt = 5;
  //  while(cnt)
  //  {
  //    printf("子进程 pid: %d, ppid: %d\n", getpid(), getppid());
  //    sleep(1);
  //    cnt--;
  //  }
  //  exit(0);
  //}
  //  sleep(10);

  //  pid_t rid = wait(NULL);
  //  if(rid > 0)
  //  {
  //    printf("wait success, rid: %d \n", rid);
  //  }
  //sleep(10);
  //return 0;
  //printf("hello1");
  //printf("hello1");
  //printf("hello1");
  //
  //fun();

  //printf("hello2");
  //printf("hello2");
  //printf("hello2");
  //
  
  //_exit(1);
  //exit(1);

 //fun();
 //printf("int main\n");
 //_exit(23);
 
 // pid_t id = fork();
 // int gval = 100;
 // if(id == 0)
 // {
 //   while(1)
 //   {
 //     printf("子: gavl:%d, &gavl:%p, pid:%d, ppid:%d\n", gval, &gval, getpid(), getppid());
 //     sleep(1);
 //     gval++;
 //   }
 // }
 // else 
 // {
 //   while(1)
 //   {
 //     
 //     printf("父: gavl:%d, &gavl:%p, pid:%d, ppid:%d\n", gval, &gval, getpid(), getppid());
 //     sleep(1);
 //   }
 //}



  //const char *str = "helloworld";
  //printf("code addr: %p\n", main);
  //printf("init global addr: %p\n", &g_val);
  //printf("uninit global addr: %p\n", &g_unval);
  //static int test = 10;
  //char *heap_mem = (char*)malloc(10);
  //char *heap_mem1 = (char*)malloc(10);
  //char *heap_mem2 = (char*)malloc(10);
  //char *heap_mem3 = (char*)malloc(10);
  //printf("heap addr: %p\n", heap_mem); //heap_mem(0), &heap_mem(1)
  //printf("heap addr: %p\n", heap_mem1); //heap_mem(0), &heap_mem(1)
  //printf("heap addr: %p\n", heap_mem2); //heap_mem(0), &heap_mem(1)
  //printf("heap addr: %p\n", heap_mem3); //heap_mem(0), &heap_mem(1)
  //printf("test static addr: %p\n", &test); //heap_mem(0), &heap_mem(1)
  //printf("stack addr: %p\n", &heap_mem); //heap_mem(0), &heap_mem(1)
  //printf("stack addr: %p\n", &heap_mem1); //heap_mem(0), &heap_mem(1)
  //printf("stack addr: %p\n", &heap_mem2); //heap_mem(0), &heap_mem(1)
  //printf("stack addr: %p\n", &heap_mem3); //heap_mem(0), &heap_mem(1)
  //printf("read only string addr: %p\n", str);
  //
  //for(int i = 0 ;i < argc; i++)
  //{
  //  printf("argv[%d]: %p\n", i, argv[i]);
  //}
  //for(int i = 0; env[i]; i++)
  //{
  //  printf("env[%d]: %p\n", i, env[i]);
  //} 
  //return 0;
}
